import java.util.Iterator;
import java.util.List;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import friendShips.User;



@Path("/register")

public class newUser {
@GET
@Produces("text/html")
public String writeNew(@QueryParam("userName") @DefaultValue("no") String userName,@QueryParam("password") @DefaultValue("0") String password) {

	SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
	Session session = sessionFactory.openSession();
	UserManager manager = new UserManager(session);
	
/*	User user1 = new User();
	 user1.setFirstName("Tony");
	 user1.setLastName("Lover");
	 user1.setEmail("jj");
	 user1.setLocation("32,36");
	 user1.setPassword("1");
	 user1.setOnline(true);
	 manager.saveUser(user1);
	 session.flush();*/
	
	
	
	Query query = session.createQuery("from User user where user.email = :newEmail");
	query.setParameter("newEmail", userName);
	List <User>list = query.list();
	Iterator<User> it=list.iterator(); // check if EMAIL exists
	if (list.size()!=0)
		return "BAD";

	
	User user = new User(); // create the new user
	user.setFirstName(userName);
	user.setLastName("Lover");
	user.setEmail(userName);
	user.setLocation("32.000000,35.000000");
	user.setPassword(password);
	user.setOnline(true);
	
	
	manager.saveUser(user);
	System.out.println("User saved with ID = "+user.getUserId());
	session.flush();
	
	return "OK";

}
}


